package jzoffer;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * Author:   dengxin
 * Date:     2020/6/12 9:59 上午
 * 构建乘积数组 B[i]=A[0]*A1]*A[2]*...*A[i-1]*A[i+1]*...A[n-1]
 */
@SuppressWarnings("all")
public class jz51 {

    public static void main(String[] args) throws Exception {
        int[] A = {1,2,3,4,5};
        int[] B = multiplay(A);
        for (int b:B) {
            System.out.println(b);
        }
    }

    public static int[] multiplay(int[] A) throws Exception {
        int length = A.length;
        int[] ret = new int[length];
        //边界条件
        if (A.length == 0) {
            throw new Exception("Empty Input Arrays");
        }
        //依次算出b左下三角的值
        ret[0] = 1;
        for (int i=1; i<A.length; i++) {
            ret[i] = ret[i-1] * A[i-1];
        }
        //左下三角值乘以右上三角，得到b
        int temp = 1;
        for (int i=A.length-1; i>=0; i--) {
            ret[i] *= temp;
            temp *= A[i];
        }
        return ret;
    }
}
